package com.itrip.controller;

import com.itrip.dto.OrderListDTO;
import com.itrip.dto.OrderPayDTO;
import com.itrip.dto.ResponseDTO;
import com.itrip.pojo.ItripHotelOrder;
import com.itrip.pojo.ItripUser;
import com.itrip.service.OrderService;
import com.itrip.vo.order.OrderAddVO;
import com.itrip.vo.order.OrderCountVO;
import com.itrip.vo.order.OrderListVO;
import io.swagger.annotations.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Api(tags = "订单相关接口")
@ApiResponses({
        @ApiResponse(code = 200,message = "请求正常",response = ResponseDTO.class)
})
@RestController
public class OrderController {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private OrderService orderService;

    public Long getUserIdToken(HttpServletRequest request){
        String token = request.getHeader("token");
        ItripUser user = (ItripUser) redisTemplate.opsForValue().get(token);
        return user.getId();
    }

    @ApiOperation("添加订单")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @PostMapping("/view/addOrder")
    public ResponseDTO addOrder(OrderAddVO orderAddVO, HttpServletRequest request){
        ItripHotelOrder order = new ItripHotelOrder();
        Long id = getUserIdToken(request);
        order.setUserId(id);
        order.setOrderType(1);
        long millis = System.currentTimeMillis();
        String s = Long.toString(millis);
        String prefix = s.substring(s.length() - 12);
        StringBuilder suffix = new StringBuilder(orderAddVO.getRoomId().toString());
        int sub = 6-suffix.length();
        if (sub > 0){
            for (int i = 0; i < sub; i++) {
                suffix.insert(0,"0");
            }
        }
        long time = orderAddVO.getCheckOutDate().getTime() - orderAddVO.getCheckInDate().getTime();
        int days = (int) (time/(1000*60*60*24));
        order.setBookingDays(days);
        order.setOrderNo(prefix+suffix);
        order.setRoomId(orderAddVO.getRoomId());
        order.setCount(orderAddVO.getCount());
        order.setCheckInDate(orderAddVO.getCheckInDate());
        order.setCheckOutDate(orderAddVO.getCheckOutDate());
        order.setOrderStatus(0);
        order.setPayAmount(BigDecimal.valueOf(orderAddVO.getPayAmount()));
        order.setNoticePhone(orderAddVO.getNoticePhone());
        order.setLinkUserName(orderAddVO.getLinkUserName());
        order.setBookType(0);
        order.setCreationDate(new Date());

        int count = orderService.addOrder(order);
        if (count > 0){

            return new ResponseDTO(100,"添加订单成功",order.getId());
        }
        return new ResponseDTO(101,"添加订单失败",null);

    }

    @ApiOperation("查询用户是否有未支付的订单")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @GetMapping("/view/getCountByUser")
    public ResponseDTO getCountByUser(HttpServletRequest request){

        Long userId = getUserIdToken(request);
        int count = orderService.getCountByUser(userId);
        if (count > 0){
            return new ResponseDTO(101,"存在未支付的订单",null);
        }
        return new ResponseDTO(100,"账号下的订单状态正常",null);
    }

    @ApiOperation("获取订单支付信息")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @GetMapping("/view/getOrderPayByUser")
    public ResponseDTO getOrderPayByUser(HttpServletRequest request){

        Long userId = getUserIdToken(request);
        OrderPayDTO orderPayDTO = orderService.getOrderPayByUser(userId);
        if (orderPayDTO != null){

            return new ResponseDTO(100,"查询成功",orderPayDTO);
        }
        return new ResponseDTO(101,"未查询到订单",null);
    }
    @ApiOperation("获取订单列表")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @GetMapping("/view/getOrderListByParams")
    public ResponseDTO getOrderListByParams(OrderListVO orderListVO,HttpServletRequest request){

        Long userId = getUserIdToken(request);
        int start = (orderListVO.getPageNow()-1)*orderListVO.getPageSize();
        List<OrderListDTO> orderList = orderService.getOrderListByParams(orderListVO,userId,start);
        return new ResponseDTO(100,"查询订单列表成功",orderList);
    }

    @ApiOperation("获取订单数量")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @GetMapping("/view/getOrderCountByParams")
    public ResponseDTO getOrderCountByParams(OrderCountVO orderCountVO, HttpServletRequest request){

        Long userId = getUserIdToken(request);
        int count = orderService.getOrderCountByParams(orderCountVO,userId);
        return new ResponseDTO(100,"查询订单列表成功",count);
    }

}
